package com.fastloan.safe.mgt.root.controller;

import com.fastloan.safe.core.param.RoleTypeEnum;
import com.fastloan.safe.core.perms.Licensing;
import com.fastloan.safe.mgt.root.model.CmDefRole;
import com.fastloan.safe.mgt.root.model.CmDefRoleFun;
import com.fastloan.safe.mgt.root.service.CmDefFunService;
import com.fastloan.safe.mgt.root.service.CmDefRoleFunService;
import com.fastloan.safe.mgt.root.service.CmDefRoleService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.spring.valid.core.BeanValid;
import com.kawins.spring.valid.group.Group;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/17 18:02
 */
@RestController
@RequestMapping("/api/web/cmDefRole")
public class CmDefRoleController extends BaseController<CmDefRole> {

    @Autowired
    private CmDefRoleService defRoleService;

    @Autowired
    private CmDefFunService defFunService;

    @Autowired
    private CmDefRoleFunService defRoleFunService;

    /**
     * 系统角色分页列表
     *
     * @return
     */
    @RequestMapping("/pageList/sys")
    @RequiresPermissions("all:/web/cmDefRole/pageList/sys")
    @Licensing({"add#post:/web/cmDefRole/sys", "update#put:/web/cmDefRole/sys", "del#delete:/web/cmDefRole/cmDefRoleId/sys"})
    public ResultTo pageListSys() {
        Map<String, Object> map = new HashMap<>();
        map.put("isAdmin", 1);
        return dataPage(map);
    }


    /**
     * 更新或者增加页面数据
     *
     * @param defRoleId
     * @return
     */
    @GetMapping("/param/sys")
    @RequiresPermissions(value = {"post:/web/cmDefRole/sys", "put:/web/cmDefRole/sys"}, logical = Logical.OR)
    public ResultTo paramSys(@RequestParam(required = false) String defRoleId) {
        Map<String, Object> map = new HashMap<>(0);
        map.put("indexTypeList", RoleTypeEnum.selectByGroup(RoleTypeEnum.Group.GROUP_SYS));
        if (StringUtils.isBlank(defRoleId)) {
            map.put("defFunTree", defFunService.selectAll2Tree(true));
        } else {
            map.put("defRole", adaptService.selectByPrimaryKey(defRoleId));
            map.put("defFunTree", defFunService.selectAll2TreeCheck(defRoleId, true));
        }
        return new ResultTo().setData(map);
    }

    /**
     * 新增系统角色
     *
     * @param cmDefRole
     * @param bindingResult
     * @param defFunIds
     * @return
     */
    @PostMapping("/sys")
    @BeanValid
    @RequiresPermissions("post:/web/cmDefRole/sys")
    public ResultTo insertSys(@Validated(Group.Insert.class) CmDefRole cmDefRole, BindingResult bindingResult,
                              String[] defFunIds) {
        cmDefRole.setIsAdmin(1);
        cmDefRole.setIsDef(0);
        cmDefRole.setIndexType(RoleTypeEnum.SYS_ROOT.getCode());
        defRoleService.insertWithFun(cmDefRole, defFunIds);
        return new ResultTo();
    }

    /**
     * 更新系统角色
     *
     * @param cmDefRole
     * @param defFunIds
     * @return
     */
    @PutMapping("/sys")
    @BeanValid
    @RequiresPermissions("put:/web/cmDefRole/sys")
    public ResultTo updateSys(@Validated(Group.Update.class) CmDefRole cmDefRole, BindingResult bindingResult,
                              String[] defFunIds) {
        //TODO:后期放开
//        CmDefRole oldRole = adaptService.selectByPrimaryKey(cmDefRole.getId());
//        if ("root".equals(oldRole.getName())) {
//            return new ResultTo(ResultEnum.OPERATION_FAILED, "无法编辑内置角色");
//        }
        cmDefRole.setIsAdmin(null);
        cmDefRole.setIsDef(null);
        defRoleService.updateWithFun(cmDefRole, defFunIds);
        return new ResultTo();
    }

    /**
     * 删除系统角色
     *
     * @param cmDefRoleId
     * @return
     */
    @Delete("/{cmDefRoleId}/sys")
    @RequiresPermissions("delete:/web/cmDefRole/cmDefRoleId/sys")
    public ResultTo delete(@PathVariable String cmDefRoleId) {
        CmDefRole cmDefRole = adaptService.selectByPrimaryKey(cmDefRoleId);
        if ("root".equals(cmDefRole.getName())) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "Root角色不能删除");
        }
        adaptService.deleteByPrimaryKey(cmDefRoleId);
        defRoleFunService.delete(new CmDefRoleFun().setDefRoleId(cmDefRoleId));
        return new ResultTo();
    }


    /**
     * 默认分页列表
     *
     * @return
     */
    @RequestMapping("/pageList/def")
    @RequiresPermissions("all:/web/cmDefRole/pageList/def")
    @Licensing({"add#post:/web/cmDefRole/def", "update#put:/web/cmDefRole/def", "del#delete:/web/cmDefRole/cmDefRoleId/def"})
    public ResultTo pageListDef() {
        Map<String, Object> map = new HashMap<>();
        map.put("isAdmin", 0);
        return dataPage(map);
    }


    /**
     * 更新或者增加页面数据
     *
     * @param defRoleId
     * @return
     */
    @GetMapping("/param/def")
    @RequiresPermissions(value = {"post:/web/cmDefRole/def", "put:/web/cmDefRole/def"}, logical = Logical.OR)
    public ResultTo paramDef(@RequestParam(required = false) String defRoleId) {
        Map<String, Object> map = new HashMap<>(0);
        map.put("indexTypeList", RoleTypeEnum.selectByGroup(RoleTypeEnum.Group.GROUP_USER, RoleTypeEnum.Group.GROUP_CUSTOMER));
        if (StringUtils.isBlank(defRoleId)) {
            map.put("defFunTree", defFunService.selectAll2Tree(false));
        } else {
            map.put("defRole", adaptService.selectByPrimaryKey(defRoleId));
            map.put("defFunTree", defFunService.selectAll2TreeCheck(defRoleId, false));
        }
        return new ResultTo().setData(map);
    }

    /**
     * 新增默认角色
     *
     * @param cmDefRole
     * @param defFunIds
     * @return
     */
    @PostMapping("/def")
    @BeanValid
    @RequiresPermissions("post:/web/cmDefRole/def")
    public ResultTo insertDef(@Validated(Group.Insert.class) CmDefRole cmDefRole, BindingResult bindingResult,
                              String[] defFunIds) {
        cmDefRole.setIsAdmin(0);
        defRoleService.insertWithFun(cmDefRole, defFunIds);
        return new ResultTo();
    }

    /**
     * 更新默认角色
     *
     * @param cmDefRole
     * @param defFunIds
     * @return
     */
    @PutMapping("/def")
    @BeanValid
    @RequiresPermissions("put:/web/cmDefRole/def")
    public ResultTo updateDef(@Validated(Group.Update.class) CmDefRole cmDefRole, BindingResult bindingResult,
                              String[] defFunIds) {
        cmDefRole.setIsAdmin(null);
        defRoleService.updateWithFun(cmDefRole, defFunIds);
        return new ResultTo();
    }

    /**
     * @param cmDefRoleId
     * @return
     */
    @DeleteMapping("/{cmDefRoleId}/def")
    @RequiresPermissions("delete:/web/cmDefRole/cmDefRoleId/def")
    public ResultTo updateDef(@PathVariable String cmDefRoleId) {
        CmDefRole cmDefRole = adaptService.selectByPrimaryKey(cmDefRoleId);
        if (cmDefRole.getIsAdmin().equals(1)) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "非法操作");
        }
        adaptService.deleteByPrimaryKey(cmDefRoleId);
        defRoleFunService.delete(new CmDefRoleFun().setDefRoleId(cmDefRoleId));
        return new ResultTo();
    }

}
